<?php
//用户资源、素材
class Resources extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->model("resources_model");
	}
	
	public function index(){
		$this->lists();
	}
	//所有素材
	public function lists(){
		$data = array(
			'resources' => $this->resources_model->lists(array('uid'=>$this->session->userdata('id')))
		);
		$this->load->view("templates/header");
		$this->load->view("resources/index",$data);
		$this->load->view("templates/footer");
	}
	
	////添加文本素材
	public function addText(){
	
		$this->load->helper('form_validation');
		
		if($this->form_validation->run() == FALSE){
			$this->lists();
		}else{
		
			$data = array(
				'uid' => $this->session->userdata('id'),
				'type' => (int)$this->input->post('type'),
				'subject' => $this->input->post('subject'),
				'content' => $this->input->post('content')
			);
			$this->resources_model->add($data);
			
		}
	
	}
	
	//添加图像素材
	public function addImage(){
		$this->session->set_userdata('tabselectedobj','#image');
		$this->load->library('form_validation');
		
		$rules = array(
				array(
					'field' => 'subject',
					'label' => 'subject',
					'rules' => 'trim|required|xss_clean'
				),				
				array(
					'field' => 'userfile',
					'label' => 'userfile',
					'rules' => 'trim|required'
				)
			
		);
		$this->form_validation->set_rules($rules);
		

		if( $this->form_validation->run() == FALSE ){
			$this->lists();
		}else{
			$this->load->model('upload_model');
			$img = $this->upload_model->upload();
			
			$data = array(
				'uid' => $this->session->userdata('id'),
				'type' => (int)$this->input->post('type'),
				'subject' => $this->input->post('subject'),
				'content' => $img['file_name']
			);
			$this->resources_model->add($data);
		}
	}
	
	//删除素材
	public function delete(){
	
	}
	
	//编辑素材
	public function eidte(){
	
	}
}

?>